export function wxLogin() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        resolve(res.code)
      },
      fail: (res) => {
        reject(res)
      }
    })
  })
}

export function request(url, data, method = 'GET') {
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      method,
      data,
      header: {
        Authorization: wx.getStorageSync('token') || ''
      },
      success: (res) => {
        // res.data 是接口返回值数据
        const {
          code,
          message,
          data
        } = res.data
        if (code === 1000) {
          // 成功
          resolve(data)
        } else {
          // 失败
          wx.showToast({
            icon: 'none',
            title: message,
          })
          reject(res.data)
        }
      },
      fail: (res) => {
        reject(res)
      }
    })
  })
}